package cn.psvmc.cxzapi.service.system;

import cn.psvmc.cxzapi.config.mybatisplus.service.impl.AbstractService;
import cn.psvmc.cxzapi.exception.ServiceException;
import cn.psvmc.cxzapi.mapper.system.TMenuMapper;
import cn.psvmc.cxzapi.mapper.system.TRoleMenuMapper;
import cn.psvmc.cxzapi.model.common.SearchEntity;
import cn.psvmc.cxzapi.model.system.TMenu;
import cn.psvmc.cxzapi.model.system.TRoleMenu;
import cn.psvmc.cxzapi.util.ToolUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 菜单表 服务实现类
 * @Author auto
 * @Date 2024-08-11
 */
@Service
public class TMenuService extends AbstractService<TMenuMapper, TMenu> {

    @Resource
    private TMenuMapper tMenuMapper;
    @Resource
    private ServiceUtil serviceUtil;
    @Resource
    private TRoleMenuService tRoleMenuService;
    @Resource
    private TRoleMenuMapper tRoleMenuMapper;

    /**
    * @Date auto 2024-08-11 新增菜单表
    * @Param tMenu 菜单表
    * @Return int
    */
    @Transactional(rollbackFor = Exception.class)
    public void add(TMenu tMenu) {
        Integer pid = 0;
        if (pid.equals(tMenu.getMenupid())) tMenu.setMenupid(null);
        tMenuMapper.insert(tMenu);
        Integer menuid = tMenu.getMenuid();
        Integer menutype = tMenu.getMenutype();
        List<TRoleMenu> roleMenus = new ArrayList<>();

        //内置平台超管
        TRoleMenu tRoleMenu1 = new TRoleMenu();
        String operates = tMenu.getOperates();
        tRoleMenu1.setRoleid(1);
        tRoleMenu1.setMenuid(menuid);
        tRoleMenu1.setOperates(operates);

        //内置待审核商家
        TRoleMenu tRoleMenu2 = new TRoleMenu();
        tRoleMenu2.setRoleid(2);
        tRoleMenu2.setMenuid(menuid);
        tRoleMenu2.setOperates(operates);

        //内置普通商家
        TRoleMenu tRoleMenu3 = new TRoleMenu();
        tRoleMenu3.setRoleid(3);
        tRoleMenu3.setMenuid(menuid);
        tRoleMenu3.setOperates(operates);
        if (menutype.equals(1)) {//平台
            roleMenus.add(tRoleMenu1);
        } else if (menutype.equals(2)) {//商家
            roleMenus.add(tRoleMenu2);
            roleMenus.add(tRoleMenu3);
        } else {//平台商家
            roleMenus.add(tRoleMenu1);
            roleMenus.add(tRoleMenu2);
            roleMenus.add(tRoleMenu3);
        }
        tRoleMenuService.saveBatch(roleMenus);
    }

    /**
    * @Date auto 2024-08-11 更新菜单表
    * @Param tMenu 菜单表模型
    * @Return int
    */
    @Transactional(rollbackFor = Exception.class)
    public void edit(TMenu tMenu) {
        // 通过id查询数据处理 详情
        TMenu originTMenu = this.detail(tMenu.getMenuid());
        if(originTMenu == null){
            throw new ServiceException("未查询到对应的菜单表信息！");
        }

        // 比较数据版本号
        if(!originTMenu.getRversion().equals(tMenu.getRversion())){
            throw new ServiceException("数据已更新，请刷新后重试！");
        }
        Integer pid = 0;
        if (pid.equals(tMenu.getMenupid())) tMenu.setMenupid(null);

        // 字段为null 不更新
        tMenuMapper.updateById(tMenu);

        //重新生成内置角色菜单
        tRoleMenuMapper.deleteByRoleIds("1,2,3");//批量删除角色权限
        LocalDateTime now = LocalDateTime.now();
        tRoleMenuMapper.saveRoleMenus(1, "1,3", now);//保存内置角色权限
        tRoleMenuMapper.saveRoleMenus(2, "2,3", now);//保存内置角色权限
        tRoleMenuMapper.saveRoleMenus(3, "2,3", now);//保存内置角色权限
    }

    /**
    * @Date auto 2024-08-11 查询菜单表详情
    * @Param id 唯一id
    * @Return cn.psvmc.cxzapi.entity.system.TMenu
    */
    public TMenu detail(Integer id){
        return tMenuMapper.selectById(id);
    }

    /**
    * @Date auto 2024-08-11 删除菜单表
    * @Param id 唯一id
    * @Param rversion 版本号
    * @Return int
    */
    public int delete(Integer id, Integer rversion){
        // 查询菜单表详情
        TMenu tMenu = this.detail(id);
        if(tMenu == null){
            throw new ServiceException("未查询到对应的菜单表信息！");
        }

        // 比较数据版本号
        if(!rversion.equals(tMenu.getRversion())){
            throw new ServiceException("数据已更新，请刷新后重试！");
        }

        return tMenuMapper.deleteById(id);
    }

    /**
     * @Description 查询菜单表列表
     * @Date 2024/8/11 21:12
     * @Author YWX
     * @Param []
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String, Object>> listAll(SearchEntity entity) {
        List<Map<String, Object>> list = tMenuMapper.listAll(entity);
        return ToolUtils.buildTree(list, "menuid", "menupid", "childList");
    }

    /**
     * @Description 获取父级菜单列表
     * @Date 2024/8/20 16:51
     * @Author YWX
     * @Param []
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String, Object>> listParent() {
        SearchEntity entity = new SearchEntity();
        return tMenuMapper.listParent(entity);
    }
}
